package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.SearcheObject;
import cn.wolfcode.trip.base.service.ICollectService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("collects")
public class CollectController {
    @Autowired
    private ICollectService collectService;

    /**
     * 查询是否收藏了
     */
    @GetMapping("/code")
    @ResponseBody
    public Map get(Long strategydetailId) {
        HashMap<String, Object> map = new HashMap<>();
        User u = UserContext.getCurrentUser();
        Long uId = u.getId();
        if (uId == null) {
            map.put("code", false);
            return map;
        }
        Collect c = collectService.get(strategydetailId);
        if (c == null) {
            map.put("code", false);
        } else {
            map.put("code", true);
        }
        return map;
    }

    /**
     * 增加或删除收藏关系
     */
    @PostMapping
    @ResponseBody
    public void post(Boolean collect, Long strategydetailId) {
        if (collect == true) {//如果返回的标签是true,就是收藏了，就需要删除关系
            collectService.delete(strategydetailId);
        } else {
            /*//假的用户Id,用来接口测试用
            Collect c = new Collect();
            c.setUserId(1L);*/

            User currentUser = UserContext.getCurrentUser();
            Collect c = new Collect();
            c.setUserId(currentUser.getId());
            c.setStrategydetailId(strategydetailId);
            collectService.save(c);
        }
    }
    /**
     * 查询收藏的数量
     */
    @GetMapping("/count")
    @ResponseBody
    public Map getByCount(){
        HashMap<String, Object> count = new HashMap<>();
        int strategyCollectCount = collectService.selectForCount();
        count.put("strategyCollectCount",strategyCollectCount);
        return count;
    }
    /**
     * 查询收藏的数据
     */
    @GetMapping
    @ResponseBody
    public PageInfo query(SearcheObject qo){
        if (qo.getType() == 1){
            qo.setPageSize(4);
            PageInfo strategyDetailPageInfo = collectService.query(qo);
            return strategyDetailPageInfo;
        }else if (qo.getType() == 2){
            /*qo.setPageSize(4);
            return collectService.query(qo);*/
        }
        return null;
    }
}
